<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/15
 * Time: 21:50
 */

namespace app\admin\controller;


use app\common\bussiness\ArticleBusiness;
use app\Request;
use think\App;

class Article extends AdminBase
{
    // TODO: 后台的文章管理只要显示文章列表以及对文章的查看、审核、删除功能

    protected $articleBus = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->articleBus = new ArticleBusiness();
    }

    /**
     * 文章列表
     * @return \think\response\View
     */
    public function index()
    {
        try {
            $list = $this->articleBus->getList();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        return view('article/index', compact('list'));
    }

    /**
     * 根据id查看文章详情
     * @param $id
     * @return \think\response\View
     */
    public function read($id)
    {
        try {
            $article_one = $this->articleBus->getOneArticleById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        return view('article/read', compact('article_one'));
    }

    public function check()
    {
        // todo: 改变现有状态值 !
    }

    public function delete(Request $request)
    {
        $id = $request->post('id', 0, 'intval');
        try {
            $res = $this->articleBus->deleteById($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        if ($res) {
            return writeJson(1, "文章删除成功");
        }
    }
}